#include "05_stacktrace.h"

#include <sstream>

#if __cplusplus >= 202302L && __has_include(<stacktrace>)
#include <stacktrace>
#define USE_STD_STACKTRACE
#else
#define BOOST_STACKTRACE_USE_BACKTRACE
#include <boost/stacktrace.hpp>
#endif

namespace lib_wrapper {
std::string get_stacktrace() {
  std::ostringstream oss;
#ifdef USE_STD_STACKTRACE
  auto st = std::stacktrace::current();
  for (const auto &entry : st) {
    oss << entry.description() << '\n';
  }
#else
  oss << boost::stacktrace::stacktrace();
#endif
  return oss.str();
}

std::string get_stacktrace_from_exception() {
  std::ostringstream oss;
#ifdef USE_STD_STACKTRACE
  auto st = std::stacktrace::current();
  for (const auto &entry : st) {
    oss << entry.description() << '\n';
  }
#else
  oss << boost::stacktrace::stacktrace::from_current_exception();
#endif
  return oss.str();
}

} // namespace lib_wrapper
